package com.atguigu.day22.tcpsocket.s2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/27 10:48
 * @注释
 */
public class Server2 {
    public static void main(String[] args) {
        //创建服务器对象
        try (ServerSocket server = new ServerSocket(11111);
             //获取字节输入流 使用Scanner进行连接
             Socket socket = server.accept();
            Scanner sc = new Scanner(socket.getInputStream());
             //获取字节输入流
            OutputStream os = socket.getOutputStream();
            //转为打印流
            PrintStream ps = new PrintStream(os)
        ) {


            //写出数据
            while (sc.hasNextLine()) {
                //读取数据并展示
                String s = sc.nextLine();
                StringBuilder reverse = new StringBuilder(s).reverse();
                ps.println("客户端发来的信息是:"+s+" ,反转后:"+reverse);
                ps.println(reverse.toString());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
